package nsn.message.aggregator.views.sources;

import nsn.message.aggregator.model.IEntry;
import nsn.message.aggregator.model.IMessageAggregator;
import nsn.message.aggregator.model.ISource;
import nsn.message.aggregator.model.ISourceType;

import org.eclipse.core.runtime.IAdapterFactory;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.model.IWorkbenchAdapter;

/**
 * @author Lukasz Nowak
 * @since 10-03-2011
 * 
 */
public class AdapterFactory implements IAdapterFactory {

	private final IWorkbenchAdapter _messageAggregatorAdapter = new IWorkbenchAdapter() {

		@Override
		public Object getParent(final Object o) {
			return null;
		}

		@Override
		public String getLabel(final Object o) {
			return ((IMessageAggregator) o).getClass().getName();
		}

		@Override
		public ImageDescriptor getImageDescriptor(final Object object) {
			return null;
		}

		@Override
		public Object[] getChildren(final Object o) {
			return ((IMessageAggregator) o).getSourceTypes().toArray();
		}
	};

	private final IWorkbenchAdapter _sourceTypeAdapter = new IWorkbenchAdapter() {

		@Override
		public Object getParent(final Object o) {
			return null;
		}

		@Override
		public String getLabel(final Object o) {
			final ISourceType type = (ISourceType) o;
			return type.getName() + "(" + type.getSources().size() + ")";
		}

		@Override
		public ImageDescriptor getImageDescriptor(final Object object) {
			return null;
		}

		@Override
		public Object[] getChildren(final Object o) {
			return ((ISourceType) o).getSources().toArray();
		}
	};

	private final IWorkbenchAdapter _sourceAdapter = new IWorkbenchAdapter() {

		@Override
		public Object getParent(final Object item) {
			return ((ISource) item).getType();
		}

		@Override
		public String getLabel(final Object item) {
			final ISource source = (ISource) item;
			return source.getName() + " (" + source.getEntries().size() + ")";
		}

		@Override
		public ImageDescriptor getImageDescriptor(final Object object) {
			return null;
		}

		@Override
		public Object[] getChildren(final Object item) {
			return ((ISource) item).getEntries().toArray();
		}
	};

	private final IWorkbenchAdapter _entryAdapter = new IWorkbenchAdapter() {

		@Override
		public Object getParent(final Object item) {
			return null;
		}

		@Override
		public String getLabel(final Object item) {
			return ((IEntry) item).getTitle();
		}

		@Override
		public ImageDescriptor getImageDescriptor(final Object object) {
			return null;
		}

		@Override
		public Object[] getChildren(final Object o) {
			return new Object[0];
		}
	};

	@SuppressWarnings("rawtypes")
	@Override
	public Object getAdapter(final Object adaptableObject,
			final Class adapterType) {
		if (IWorkbenchAdapter.class.isAssignableFrom(adapterType)) {
			if (adaptableObject instanceof IMessageAggregator) {
				return _messageAggregatorAdapter;
			}
			if (adaptableObject instanceof ISourceType) {
				return _sourceTypeAdapter;
			}

			if (adaptableObject instanceof ISource) {
				return _sourceAdapter;
			}
			if (adaptableObject instanceof IEntry) {
				return _entryAdapter;
			}
		}
		return null;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public Class[] getAdapterList() {
		return new Class[] { IWorkbenchAdapter.class };
	}

}
